using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Autofac;
using Autofac.Builder;
using Catastrophe.Initialize;
using Catastrophe.Initialize.IOCProvier;

namespace Catastrophe.BasePC.UI.Extend
{
    public static class CdcatastropheWfExtend
    {
        public static void RegisterForm<T>(this ContainerBuilder container, string name = "", InstanceLifeEnum instanceLife = InstanceLifeEnum.PerDependency, params object[] tags) where T : Window
        {
            string serviceName = (string.IsNullOrWhiteSpace(name) ? typeof(T).FullName : name);
            IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder = container.RegisterType<T>().As<Window>().Named<Window>(serviceName);
            switch (instanceLife)
            {
                case InstanceLifeEnum.PerDependency:
                    registrationBuilder.InstancePerDependency();
                    break;
                case InstanceLifeEnum.Single:
                    registrationBuilder.SingleInstance();
                    break;
                case InstanceLifeEnum.PerLifetimeScope:
                    registrationBuilder.InstancePerLifetimeScope();
                    break;
                case InstanceLifeEnum.PerMatchingLifetimeScope:
                    registrationBuilder.InstancePerMatchingLifetimeScope(tags);
                    break;
                case InstanceLifeEnum.PerRequest:
                    registrationBuilder.InstancePerRequest(tags);
                    break;
            }
        }

        public static IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterUserControl<T>(this ContainerBuilder container, string name = "", InstanceLifeEnum instanceLife = InstanceLifeEnum.PerDependency, params object[] tags) where T : UserControl
        {
            string serviceName = (string.IsNullOrWhiteSpace(name) ? typeof(T).FullName : name);
            IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrationBuilder = container.RegisterType<T>().As<UserControl>().Named<UserControl>(serviceName);
            switch (instanceLife)
            {
                case InstanceLifeEnum.PerDependency:
                    registrationBuilder.InstancePerDependency();
                    break;
                case InstanceLifeEnum.Single:
                    registrationBuilder.SingleInstance();
                    break;
                case InstanceLifeEnum.PerLifetimeScope:
                    registrationBuilder.InstancePerLifetimeScope();
                    break;
                case InstanceLifeEnum.PerMatchingLifetimeScope:
                    registrationBuilder.InstancePerMatchingLifetimeScope(tags);
                    break;
                case InstanceLifeEnum.PerRequest:
                    registrationBuilder.InstancePerRequest(tags);
                    break;
            }
            return registrationBuilder;
        }

        public static void RegisterSystemIni<T, TInterface>(this ContainerBuilder container, string name = "") where T : TInterface where TInterface : IRunManager
        {
            string serviceName = (string.IsNullOrWhiteSpace(name) ? typeof(T).FullName : name);
            container.RegisterType<T>().As<TInterface>().Named<TInterface>(serviceName);
        }
    }
}
